﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace NBOW
{
    public class Common
    {
        List<string> listWordofBug = new List<string>(); // List all bug
        List<string> listValueCell = new List<string>(); // Every emplement of array contains one bug report
        private static readonly char[] delimiters = { ' ', '.', ',', ';', '\'', '_', '-', ':', '!', '?', '(', ')', '<', '>', '=', '*', '/', '[', ']', '{', '}', '\\', '"', '\r', '\n' };// List character limit.
        ConnectDB connect = new ConnectDB();

        #region process word

        // process stop word
        public List<string> processWordofBug(string str_arraytext)
        {
            List<string> listText = new List<string>();// List contains stop word
            listText = GetWordLimited();// call stop word file
            List<string> listTemp = new List<string>();

            // process string with delimiters
            if (!string.IsNullOrEmpty(str_arraytext))
            {
                while (str_arraytext != string.Empty)
                {
                    listTemp.AddRange(str_arraytext.Split(delimiters));
                    str_arraytext = "";
                }
            }

            // process string with word-stop
            for (int i = 0; i < listTemp.Count; i++)
            {
                string a = listTemp[i].ToLower();
                if (listText.Contains(a))
                {
                    listTemp.Remove(listTemp[i]);
                    i--;
                }
            }
            return listTemp;
        }

        // Read stop word from charlimit
        public List<string> GetWordLimited()
        {
            List<string> listText = new List<string>();// List contains stop word
            string path = Environment.CurrentDirectory + "\\FileDelimited\\wordlimited.txt";
            string str_line = string.Empty;
            
            // get character array
            System.IO.StreamReader file = new System.IO.StreamReader(path);
            while ((str_line = file.ReadLine()) != null)
            {
                listText.AddRange(str_line.Split('\n'));
            }

            return listText;
        }

        // process duplicate words
        public string RemoveDuplicateWords(string str_text)
        {
            var words = new HashSet<string>();
            str_text = Regex.Replace(str_text, "\\w+", m =>
                                 words.Add(m.Value.ToUpperInvariant())
                                     ? m.Value
                                     : String.Empty);
            return str_text;
        }

        #endregion

        #region check Field of Excel file

        // check empty field
        public List<int> disField(DataTable getExcel)
        {
            int int_k = 0;
            List<int> listColumns = new List<int>();// get columns empty and litle value
            if (getExcel != null)
            {
                while (int_k < getExcel.Columns.Count)
                {
                    int int_i = 0;
                    int int_j = 0;
                    foreach (DataRow r in getExcel.Rows)
                    {
                        int_j++;
                        List<string> lsTemp = new List<string>();
                        lsTemp.AddRange(r[int_k].ToString().Split(' '));
                        if (lsTemp.Count < 3)
                            int_i++;
                        if (int_i >= 10 || int_i == getExcel.Rows.Count)
                        {
                            listColumns.Add(int_k);
                            break;
                        }
                        if (int_j > 20)
                            break;
                    }
                    int_k++;
                }
            }
            return listColumns;
        }

        #endregion

        #region read and write file

        // read file
        private Dictionary<string, double> readfile(string path)
        {
            Dictionary<string, double> dicReadFile = new Dictionary<string, double>();
            string line = "";
            // get character array
            System.IO.StreamReader file = new System.IO.StreamReader(path);
            while ((line = file.ReadLine()) != null)
            {
                List<string> listWord = new List<string>();
                listWord.AddRange(line.Split(':'));
                dicReadFile.Add(listWord[0], double.Parse(listWord[1].ToString()));
            }
            file.Close();
            
            return dicReadFile;
        }

        // read BOW
        public Dictionary<string, double> readBagOfWord()
        {
            // read BOW
            Dictionary<string, double> dicBOW = new Dictionary<string, double>();
            string path = Environment.CurrentDirectory + "\\DataTest\\BagOfWords.txt";
            if (!System.IO.Directory.Exists(path))
                dicBOW = this.readfile(path);
            return dicBOW;
        }

        // read frequency
        public Dictionary<string, double> readFrequencyWord()
        {
            Dictionary<string, double> dicFre = new Dictionary<string, double>();
            string pathFre = Environment.CurrentDirectory + "\\DataTest\\WriteText.txt";
            if (!System.IO.Directory.Exists(pathFre))
                dicFre = this.readfile(pathFre);
            return dicFre;
        }

        // write new file
        public void writefile(Dictionary<string, double> dicWord, string path)
        {
            System.IO.TextWriter tw = new System.IO.StreamWriter(path);
            string temp = "";
            foreach (string item in dicWord.Keys)
            {
                if (temp != "")
                    temp += "\n";
                temp += item + ":" + dicWord[item].ToString();
            }
            tw.WriteLine(temp);
            tw.Close();
        }

        //write update file
        public void writefileupdate(Dictionary<string, double> dicWord, string path)
        {
            System.IO.TextWriter tw = new System.IO.StreamWriter(path);
            string temp = "";
            foreach (string item in dicWord.Keys)
            {
                if (temp != "")
                    temp += "\n";
                temp += item + ":" + dicWord[item].ToString();
            }
            tw.WriteLine(temp);
            tw.Close();

        }

        #endregion

    }
}
